package com.spt.springbootzhsx.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class RedisOp {
    @Autowired
    private RedisTemplate redisTemplate;

    //添加或修改键值对
    public void set(String key,String value){
        redisTemplate.opsForValue().set(key,value);
    }

    //添加或修改键值对和有效时间
    public void set(String key,String value,Integer time){
        redisTemplate.opsForValue().set(key,value,time, TimeUnit.MINUTES);
    }

    //查询键值对
    public String get(String key){
        String value=redisTemplate.opsForValue().get(key).toString();
        if(value!=null){
            return value;
        }else{
            return "无该键值对应的记录!";
        }
    }


    //删除键值对
    public boolean delete(String key){
        redisTemplate.delete(key);
        return redisTemplate.hasKey(key);
    }
}
